<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证" prop="certno">
        <el-input
          v-model="queryParams.certno"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名赛事" prop="competitionId">
          <el-select v-model="queryParams.competitionId" placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        </el-form-item>
      <el-form-item label="审核" prop="review">
        <el-select v-model="queryParams.review" placeholder="请选择赛事">
          <el-option
            v-for="item in reviewoptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="审核通过时间" prop="reviewTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.reviewTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择审核通过时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="考场" prop="exam">-->
<!--        <el-input-->
<!--          v-model="queryParams.exam"-->
<!--          placeholder="请输入考场"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="考试成绩" prop="scores">-->
<!--        <el-input-->
<!--          v-model="queryParams.scores"-->
<!--          placeholder="请输入考试成绩"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="修改日期" prop="updateTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.updateTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择修改日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="身份证号码" align="center" prop="certno" width="161" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" width="75" />
      <el-table-column label="年龄" align="center" prop="age" width="75"/>
      <el-table-column label="现住址" align="center" prop="address" width="300" />
      <el-table-column label="报名赛事" align="center" prop="competitionId" width="300" />
      <el-table-column label="报名时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核" align="center" prop="review" width="80">
      <template slot-scope="scope">
        <span v-if="scope.row.review == 0" style="color: #1156d7;">未审核</span>
        <span v-if="scope.row.review == 1 "style="color: forestgreen;">审核通过</span>
        <span v-if="scope.row.review == 2 "style="color: #e7d407;">未通过</span>
        <span v-if="scope.row.review == 3 "style="color: #e80d2f;">异常</span>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            round
            icon="el-icon-view"
            @click="handleReview(scope.row)"
            v-hasPermi="['student:review']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="身份证号码" prop="certno" >
          <el-input v-model="form.certno" placeholder="请输入证号码" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="现住址" prop="address">
          <div id="app">
            <el-cascader
              size="large"
              :options="pcaTextArr"
              v-model="form.region"
              placeholder="请选择赛事举办地区">
            </el-cascader>
          </div>
          <el-input v-model="form.address" placeholder="请输入详细住址(XX街道)" />
        </el-form-item>
        <el-form-item label="报名赛事" prop="competitionId">
          <el-select v-model="form.competitionId" placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="danger" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="update" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="身份证号码" prop="certno" >
          <el-input v-model="form.certno" placeholder="请输入证号码" :disabled="true" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="现住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细住址" />
        </el-form-item>
        <el-form-item label="报名赛事" prop="competitionId">
          <el-select v-model="form.competitionId" placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="danger" @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 审批报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="review" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="身份证号码" prop="certno">
          <el-input v-model="form.certno" placeholder="请输入姓名" readonly />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" readonly />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" readonly />
        </el-form-item>
        <el-form-item label="现住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细住址" readonly />
        </el-form-item>
        <el-form-item label="报名赛事" prop="competitionId">
          <el-select v-model="form.competitionId" placeholder="请选择赛事">
            <el-option disabled
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="审核结果" prop="review">
          <el-select v-model="form.review" placeholder="请选择审核结果">
            <el-option
                       v-for="item in reviewoptions"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id"
                       :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button type="danger" @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudent,
  getStudent,
  delStudent,
  addStudent,
  updateStudent,
  loadCompetition,
} from "@/api/student/student";
import {validateIdNumber} from "@/api/tool/tooljs"
import { pcaTextArr } from 'element-china-area-data'

export default {
  name: "Student",
  data() {

    return {
      pcaTextArr,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      reviews: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报名表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      update: false,
      review:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certno: null,
        name: null,
        sex: null,
        age: null,
        address: null,
        competitionId: null,
        createTime: null,
        review: null,
        reviewTime: null,
        exam: null,
        scores: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        certno: [
          {required: true, message: "身份证号码不能为空", trigger: "blur"},
          {validator: validateIdNumber, message: '请检查输入格式'}
        ],
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        sex: [
          {required: true, message: "性别不能为空", trigger: "change"}
        ],
        age: [
          {required: true, message: "年龄不能为空", trigger: "blur"}
        ],
        address: [
          {required: true, message: "现住址不能为空", trigger: "blur"}
        ],
        competitionId: [
          {required: true, message: "报名赛事不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "报名时间不能为空", trigger: "blur"}
        ],
        review: [
          {required: true, message: "审核不能为空", trigger: "blur"}
        ],
        reviewTime: [
          {required: true, message: "审核通过时间不能为空", trigger: "blur"}
        ],
        exam: [
          {required: true, message: "考场不能为空", trigger: "blur"}
        ],
        scores: [
          {required: true, message: "考试成绩不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "修改日期不能为空", trigger: "blur"}
        ]
      },
      options: [],
      reviewoptions: [
        {id: '0', name: '未审核' , disabled: true},
        {id: '1', name: '审核通过'},
        {id: '2', name: '未通过'},
        {id: '3', name: '异常'},],
    }

  },
  created() {
    this.getList();
    this.loadCompetition();
  },
  methods: {


    /**
     * 赛事下拉选加载
     */
    loadCompetition() {
      console.log("赛事下拉选加载");
      loadCompetition().then(response => {
        this.options = response.data
        console.log(this.options);
      });
    },
    /** 查询报名列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.update = false;
      this.review = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        certno: null,
        name: null,
        sex: null,
        age: null,
        address: null,
        competitionId: null,
        createTime: null,
        review: null,
        reviewTime: null,
        exam: null,
        scores: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.certno)
      this.reviews = selection.map(item => item.review)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.loadCompetition();
      this.title = "添加报名";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const certno = row.certno || this.ids
      const review = row.review || this.reviews
      if((review == 1)){
        this.$message({
          showClose: true,
          message: '审核通过的参赛者不可修改信息!',
          type: 'error'
        });
        return
      }
      getStudent(certno).then(response => {
        this.form = response.data;
        this.form.competitionId = Number(this.form.competitionId);
        this.loadCompetition();
        this.update = true;
        this.title = "修改报名";
      });
    },
    /** 审批按钮操作*/
    handleReview(row){
      this.reset();
      const certno = row.certno || this.ids
      const review = row.review || this.reviews
      if((review == 1)) {
        this.$message({
          showClose: true,
          message: '已经审核通过啦!',
          type: 'warning'
        });
        return;
      }
      getStudent(certno).then(response => {
        this.form = response.data;
        this.form.competitionId = Number(this.form.competitionId);
        this.form.review = String(this.form.review);
        this.loadCompetition();
        this.review = true;
        this.title = "审批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.createTime != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.update = false;
              this.review = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const certnos = row.certno || this.ids;
      this.$modal.confirm('是否确认删除报名编号为"' + certnos + '"的数据项？').then(function() {
        return delStudent(certnos);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  },


};
</script>
